const datbase = require('./datbase');
let loadAllItems = datbase.allItems();
let loadPromotions = datbase.promotions();

//统计商品数量
function allItemsCount(inputsArr){
    let obj = {};
    for(let item of inputsArr){
        if(item.indexOf("-") == -1){
            if(!obj[item]){
                obj[item] = {};
                obj[item].barcode = item;
                obj[item].count = 1;
            }
            else{
                obj[item].count ++ ;
            }
        }
        else{
            let arr = item.split("-");
            item = arr[0];
            obj[item] = {};
            obj[item].barcode = item;
            obj[item].count = parseInt(arr[1]);
        }
    }
    let countArr=[];
    for(let item in obj){
        countArr.push(obj[item]);
    }
    //console.log(countArr);
    return countArr;    
}
//赠送商品统计
function promotion(itemsCount){
    let promotionsBarcodes=loadPromotions[0].barcodes;
    let promotions = [];
    for(let item of itemsCount){
        for(let barcodesItem of promotionsBarcodes){
            if(item.barcode == barcodesItem){
                loadAllItems.forEach(function(ele){
                    if(ele.barcode==item.barcode){
                        promotions.push({name:ele.name ,count:parseInt(item.count/3) ,unit:ele.unit });
                    }
                })
                
            }
        }
    }
    //console.log(promotions);
    return promotions;
}
//统计购买的商品信息
function goodsInventory(itemsCount,promotions){
    let goodsInfo = [];
    for(let goodsItem of itemsCount){
        for(let goods of loadAllItems){
            if(goods.barcode==goodsItem.barcode){
                    goodsInfo.push({name:goods.name ,count:goodsItem.count ,unit:goods.unit ,price:goods.price.toFixed(2) ,total:goods.price*( goodsItem.count-parseInt(goodsItem.count/3) )});
            }
        }
    }
    //console.log(goodsInfo);
    return goodsInfo;
}
//计算总价
function countsummary(goodsInfo){
    let sum=0;
    for(let item of goodsInfo){
        sum+=item.total;
    }
    //console.log(sum);
    return sum;
}
//计算节省的价钱
function countSave(promotions){
    let save = 0;
    for(let item of promotions){
        for(let itemInfo of loadAllItems){
            if(item.name==itemInfo.name){
                save +=itemInfo.price*item.count;
            }
        }
    }
    //console.log(save);
    return save;
}
//输出
function printInventory(goodsInfo,promotions,summary,save){
    let result;
    result = "***<没钱赚商店>购物清单***\n";
    for(let item of goodsInfo){
        result += "名称："+item.name+"，数量："+item.count+item.unit+"，单价："+item.price+"(元)，小计："+item.total.toFixed(2)+"(元)\n";
    }
    result +="----------------------\n";
    if(promotions.length==0){
        result +="无优惠商品\n";
    }
    else{
        result +="挥泪赠送商品：\n";
        for(let proItem of promotions){
            result += "名称："+proItem.name+"，数量："+proItem.count+proItem.unit+"\n";
        }
    }
    result += "----------------------\n";
    result += "总计："+summary.toFixed(2)+"(元)\n";
    result += "节省："+save.toFixed(2)+"(元)\n";
    result += "**********************";
    return result;
}
module.exports = function main(inputs) {
    
    let itemsCount = allItemsCount(inputs);
    let promotions = promotion(itemsCount);
    let goodsInfo = goodsInventory(itemsCount,promotions);
    let summary=countsummary(goodsInfo);
    let save=countSave(promotions);
    let result = printInventory(goodsInfo,promotions,summary,save);
    console.log(result);
};